package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.enums.AbnormalEnum;
import com.ruoyi.framework.web.exception.TadpoleException;
import com.ruoyi.web.domain.Currency;
import com.ruoyi.web.domain.UserAddress;
import com.ruoyi.web.mapper.UserAddressMapper;
import com.ruoyi.web.service.ICurrencyService;
import com.ruoyi.web.service.IUserAddressService;
import com.ruoyi.web.vo.UserAddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户充值地址Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-30
 */
@Service
public class UserAddressServiceImpl implements IUserAddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private ICurrencyService currencyService;

    /**
     * 获取用户充值地址
     */
    @Override
    public R<List<UserAddressVO>> getUserAddress() {
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
            }
            List<UserAddress> addresses = userAddressMapper.selectUserAddressList(new UserAddress() {{
                setUserId(userId);
            }});
            if (addresses.size() == 0) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
            }
            List<UserAddressVO> list = new ArrayList<>();
            for (UserAddress address : addresses) {
                UserAddressVO vo = new UserAddressVO() {{
                    setAddress(address.getAddress());
                    setChain(address.getChain());
                }};
                Currency currency = currencyService.getCurrency(vo.getChain());
                BeanUtils.copyProperties(currency, vo);
                list.add(vo);
            }
            return R.ok(list);
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }

    /**
     * 查询用户充值地址
     *
     * @param id 用户充值地址主键
     * @return 用户充值地址
     */
    @Override
    public UserAddress selectUserAddressById(Long id) {
        return userAddressMapper.selectUserAddressById(id);
    }

    /**
     * 查询用户充值地址列表
     *
     * @param userAddress 用户充值地址
     * @return 用户充值地址
     */
    @Override
    public List<UserAddress> selectUserAddressList(UserAddress userAddress) {
        return userAddressMapper.selectUserAddressList(userAddress);
    }

    /**
     * 新增用户充值地址
     *
     * @param userAddress 用户充值地址
     * @return 结果
     */
    @Override
    public int insertUserAddress(UserAddress userAddress) {
        return userAddressMapper.insertUserAddress(userAddress);
    }

    /**
     * 修改用户充值地址
     *
     * @param userAddress 用户充值地址
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        return userAddressMapper.updateUserAddress(userAddress);
    }

    /**
     * 批量删除用户充值地址
     *
     * @param ids 需要删除的用户充值地址主键
     * @return 结果
     */
    @Override
    public int deleteUserAddressByIds(Long[] ids) {
        return userAddressMapper.deleteUserAddressByIds(ids);
    }

    /**
     * 删除用户充值地址信息
     *
     * @param id 用户充值地址主键
     * @return 结果
     */
    @Override
    public int deleteUserAddressById(Long id) {
        return userAddressMapper.deleteUserAddressById(id);
    }

    /**
     * 根据userIdList查询
     *
     * @param userIdList 用户idList
     * @param condition  查询条件
     * @return {@link List< UserAddress>}
     * @since 2023/4/4 12:37
     */
    @Override
    public List<UserAddress> listByUserIdList(List<Long> userIdList, UserAddress condition) {
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>(0);
        }

        return userAddressMapper.listByUserIdList(userIdList, condition);
    }
}
